<?php
namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\data\Pagination;

/**
 * 队列管理相关类
 *
 * @author yumao
 */

class QueueSystemEmailManageMqController extends \app\base\Controller
{
        
    // 队列列表
    public function actionIndex()
    {
        $data = [];
        $siteInfo = [];
        $defaultPageSize = 50;
        $pageSize = 50;
        $mothod = strtolower(Yii::$app->request->getMethod());
        $site = Yii::$app->request->$mothod('site');
        
        // 查询网站相关的数据
        $formData = Yii::$app->request->$mothod();
        
        // 当前用户所有有权限的站点
        $siteInfo_tmp = Yii::$app->authority->getSysEmailAccessSite();
        $siteInfo[0] = '网站';
        if (isset($siteInfo_tmp) && is_array($siteInfo_tmp)) {
            foreach ($siteInfo_tmp as $siteInfo_tmp_k => $siteInfo_tmp_v) {
                $siteInfo[$siteInfo_tmp_v['name']] = $siteInfo_tmp_v['name'];
            }
        }

        // 当前用户所有有权限的站点结束
        $formData['siteInfo'] = $siteInfo;
        
        // 队列名相关
        $queueInfo = [];
        $queueInfo[0] = '请选择队列';
        $formData['site_name'] = isset($formData['site_name']) ? $formData['site_name'] : 0;
        if ($formData['site_name']) {
            $site_name = $formData['site_name'];
        } else {
            $site_name = '';
        }
       
        //$formData['queue_id'] = isset($formData['queue_id']) ? $formData['queue_id'] : 0;
        $page = Yii::$app->request->$mothod('page') ? Yii::$app->request->$mothod('page') : 1;
        $pageSize = Yii::$app->request->$mothod('pagesize') ? Yii::$app->request->$mothod('pagesize') : $defaultPageSize;
        $formData['page'] = $page;
        $formData['pageSize'] = $pageSize;
        
        // 判断是否有当前网站的权限
        if ($formData['site_name']) {
            $flag = Yii::$app->authority->judgeSysEmailHaveSite($formData['site_name']);
            if (!$flag) {
                 $this->sysMsg('警告', '你的账号没有权限!',$params = [ 'type' => 'error', 'links' => [ ['name'=>'返回上一页' , 'url'=>'javascript:history.go(-1)'] ] ]);
            }
        } 
        // 队列名结束
       
        // 查询相关数据
        if (!empty($formData['site_name'])) {
            // 查询rabbitmq中相关信息
            $obj = 'app\components\queuemq\QueueMqSysEmail' . ucwords($site_name);
            $queueObj = new $obj();
            $queueObj->getQueueData($formData);
            echo '这是队列中的相关信息';
            exit;
            $data_tmp = $queueObj->getQueueData($formData);
            $data = $data_tmp['data'];
            $totalCount = $data_tmp['length'];
        } else {
            $totalCount = 0;
        }
        // 查询数据结束
       
        // 分页相关
        $pages = new Pagination(['totalCount' =>$totalCount, 'pageSize' => $pageSize, 'defaultPageSize' => $pageSize]);
        $pageCount = $pages->getPageCount(); //页数

        //$pageOther = ['totalCount' => $totalCount, 'pageCount' => $pageCount];
        // 分页相关结束
        // 渲染模版
        return $this->render('list',[
            'data' => $data,
            'site' => $site,
            'site_name' => $site_name,
            'pages' => $pages,
            //'pageOther' => $pageOther,
            'formData' => $formData,
            // 'curPageIds' => $curPageIds,           
        ]);
    }

    // 根据键名查询某一队列
    // public function actionSearchByKey()
    // {
        
    // }
}
